package com.easy.controller;


import com.easy.bean.UserBean;
import com.easy.service.UserBeanService;
import com.easy.util.Page;
import com.easy.util.Result;
import com.easy.util.SYS;
import com.easy.util.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {

    @Autowired
    UserBeanService userBeanService;

    @PostMapping
    public Result add(@RequestBody UserBean userBean){
        Object obj=userBeanService.add(userBean);
        return Result.success(obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        int obj=userBeanService.del(id);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody UserBean userBean){
        Object obj=userBeanService.edit(userBean);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(UserBean userBean){
        Object obj=userBeanService.all(userBean);
        return Result.success(obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj=userBeanService.one(id);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Page page, UserBean userBean){
        userBeanService.page(page,userBean);
        return Result.success(page);
    }

    @PutMapping("changePassword")
    public Result changePassword(@RequestParam("user_code") String oldPassword, @RequestParam("newPassword") String newPassword,
            @RequestParam("confirmPassword") String confirmPassword,
            HttpServletRequest request
    ) {
        // 1. 从请求头获取 Token
        String token = request.getHeader(SYS.TOKEN_AUTHORIATION);
        System.out.println(token);

        return userBeanService.changePassword(oldPassword, newPassword, confirmPassword,token);
    }

}
